<?php
class SysnavAction extends BaseAction{
    
	function index()
	{
		$Dao = D("Sysnav"); 
		$count = $Dao->count(); 
		import("ORG.Util.Page"); 
		if(!empty($_REQUEST["listRows"])) { 
			$listRows = $_REQUEST["listRows"]; 
			}else{ 
			$listRows = 20; 
		} 
		$p = new Page($count,$listRows); 
 
		$field = "*"; 
		$list = $Dao->findAll("",$field,"orderNum asc",$p->firstRow.",".$p->listRows); 
		$page = $p->show(); 
		
		$list = $Dao->toTree($vo,'id','fid','_child');
		$this->treetoary($list);
//dump($this->tree);
		$this->assign("list",$this->tree); 
		$this->assign("page",$page); 
		$this->display(); 
	}

	function add() 
	{ 
		$Dao = D("Sysmodule"); 
		$this->moduleID = $Dao->where("status=1")->findAll();
		//dump($this->module);
		// 添加内容的页面 
		$this->display(); 
	} 

	function insert() 
	{ 
		$Dao = D("Sysnav"); 
		$vo = $Dao->create(); 
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		}
		if(!empty($_FILES['file1']['name'])){
            $file = $this->upload();
            //dump($file);exit;
			$Dao->rest1 = $file[0];//赋值
			$Dao->rest2 = $file[1];//赋值
			$Dao->rest3 = $file[2];//赋值
		}	
		
		$rs = $Dao->add(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("添加数据成功!"); 
		}else{ 
			$this->error("添加数据失败"); 
		} 
	}

	function edit() 
	{ 
		$Dao = D("Sysmodule"); 
		$this->moduleID = $Dao->where("status=1")->findAll();

		// 保存添加的数据
		unset($Dao);
		// 编辑页面 
		$Dao = D("Sysnav"); 
		$vo = $Dao->find("id=".$_GET["id"]); 
		if($vo === false) 
		{ 
			$this->assign("jumpUrl",__URL__); 
			$this->error("Non-existed record"); 
		} 
		 
		$this->assign("vo",$vo); 
		$this->display(); 
	} 

	function update() 
	{ 
		// 保存编辑过的数据 
		$Dao = D("Sysnav"); 
		$vo = $Dao->create("","edit");
		if(false === $vo) { 
	 		$this->error($Dao->getError()); 
		}	 
 		if(!empty($_FILES['file1']['name'])){
            $file = $this->upload();
            //dump($file);exit;
			$Dao->rest1 = $file[0];//赋值
			$Dao->rest2 = $file[1];//赋值
			$Dao->rest3 = $file[2];//赋值
		}		
		$rs = $Dao->save(); 
		if($rs){ 
			$this->assign("jumpUrl",__URL__); 
			$this->success("保存修改成功!"); 
		}else{ 
			$this->error("保存修改失败"); 
		} 
	}

	function delete() 
	{ 
		// 根据id删除指定的记录
		$Dao = D("Sysnav"); 
		$id = $_REQUEST["id"];
		if(isset($id)) { 
			$result = $Dao->getById($id); 

			//-------------检测模块下面是否有数据----------开始>
			$moduleName = getSysmoduleName($result['moduleID']);
			$count = D($moduleName)->count("nid=$id");
			$count += D("Category")->count("nid=$id");
			//dump($Dao->getLastSql());
			//dump($count);
			if($count > 0 ){
				$this->jumpUrl = __APP__."/$moduleName/index/nid/".$id;//返回含有数据的栏目
				$this->error($Dao->name.'栏目下面还有数据,请先清空数据.'); 
			}
			//--------------检测模块下面是否有数据-----------结束>
			
			//------------删除栏目的附件-----------开始>
			if($Dao->rest1) unlink($Dao->rest1);
			if($Dao->rest2) unlink($Dao->rest2);
			if($Dao->rest3) unlink($Dao->rest3);
			//------------删除栏目的附件-----------开始>
			
			//dump($result);
			//dump($count);
			//dump(getModuleName($result['id']));
			//exit;
			if(!$result) { 
				$this->error("Non-existed record!"); 
			} 
			if($Dao->deleteById($id)){ 
				$this->assign("jumpUrl",__URL__); 
				$this->success("删除数据成功"); 
			}else { 
				$this->error("删除数据失败"); 
			}
		}else { 
			$this->error("非法操作"); 
		} 
	}
	
	var $tree=array();
	function treetoary($list,$level=0) {
	  foreach($list as $key=>$val)
	  {
		$tmp_str=str_repeat("&nbsp;",$level*2);
		if($level>0) {
			$tmp_str.="├";
		}
		$val['name']=$tmp_str."&nbsp;".$val['name'];
		if(!array_key_exists('_child',$val)) {
		   array_push($this->tree,$val);
		}else{
			$tmp_ary = $val['_child'];
			unset($val['_child']);
			array_push($this->tree,$val);
			$this->treetoary($tmp_ary,$level+1);
		}
	  }
	  return;
	}

}
?>